-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(storage): allow for Age int64* type and int64 type #6230
Conversation
a063185
to
5df45dc
Compare
5df45dc
to
8a9137c
Compare
…loud-go into reflect-condition-field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice, thanks for adding this Frank!
storage/bucket.go
Outdated
v := reflect.ValueOf(cond).Elem().FieldByName("Age") | ||
if v.Kind() == reflect.Int64 { | ||
return v.Interface().(int64) | ||
} else if v.Kind() == reflect.Pointer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pointer
got added in a recent Go release; use reflect.Ptr
here instead. (This is why the CI fails.)
storage/bucket.go
Outdated
@@ -1503,6 +1503,17 @@ func toCORSFromProto(rc []*storagepb.Bucket_Cors) []CORS { | |||
return out | |||
} | |||
|
|||
// Handle ptr or int64 | |||
func setAgeCondition(age int64, cond interface{}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a note about why this is needed and a TODO/issue to remove in a future release.
storage/bucket.go
Outdated
@@ -1525,6 +1536,8 @@ func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle { | |||
}, | |||
} | |||
|
|||
setAgeCondition(r.Condition.AgeInDays, rr.Condition) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setAgeCondition(r.Condition.AgeInDays, rr.Condition) | |
setAgeCondition(r.Condition.AgeInDays, &rr.Condition) |
storage/bucket_test.go
Outdated
var tp testPointer | ||
var want int64 = 10 | ||
setAgeCondition(want, &tp) | ||
if getAgeCondition(&tp) != want { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if getAgeCondition(&tp) != want { | |
if getAgeCondition(tp) != want { |
storage/bucket_test.go
Outdated
var ti testInt64 | ||
want = 100 | ||
setAgeCondition(100, &ti) | ||
if getAgeCondition(&ti) != want { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if getAgeCondition(&ti) != want { | |
if getAgeCondition(ti) != want { |
storage/bucket_test.go
Outdated
var want int64 = 10 | ||
setAgeCondition(want, &tp) | ||
if getAgeCondition(&tp) != want { | ||
t.Fatalf("got %v, want 10", *tp.Age) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.Fatalf("got %v, want 10", *tp.Age) | |
t.Fatalf("got %v, want %v", *tp.Age, want) |
storage/bucket.go
Outdated
@@ -1503,6 +1503,17 @@ func toCORSFromProto(rc []*storagepb.Bucket_Cors) []CORS { | |||
return out | |||
} | |||
|
|||
// Handle ptr or int64 | |||
func setAgeCondition(age int64, cond interface{}) { | |||
c := reflect.ValueOf(cond).Elem().FieldByName("Age") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would limit the amount of reflection needed by just passing in a pointer to the field directly -- do a recursive call if needed to go one more level in.
storage/bucket.go
Outdated
@@ -1595,6 +1608,19 @@ func toProtoLifecycle(l Lifecycle) *storagepb.Bucket_Lifecycle { | |||
return &rl | |||
} | |||
|
|||
// Handle ptr or int64 | |||
func getAgeCondition(cond interface{}) int64 { | |||
v := reflect.ValueOf(cond).Elem().FieldByName("Age") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for this one, I would just pass in the field directly.
Some of my comments might not make sense if you refactor the getter/setter into working directly on fields FYI. Feel free to dismiss those |
Also forgot to ask, how much of a hot path is this code on for normal operations? There is be a penalty for temporarily using reflection. |
Anytime a customer gets Bucket.Attrs(), it will go through this path iff OLM policies are set on the bucket. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for all the changes!
Towards unblocking: googleapis/google-cloud-go#6230 Change Storage OLM Age condition type from int64 to *int64 to allow for existence of check Co-authored-by: Chris Cotter <cjcotter@google.com>
@Albert-Gao I believe the error you are seeing is from having incompatible versions of cloud.google.com/go/storage and google.golang.org/api. Also, I think it is an issue with your project's build rather than with firebase.google.com/go/v4 because the deps for the firebase package predate this change. I would try upgrading both cloud.google.com/go/storage and google.golang.org/api to the most recent version, or downgrading google.golang.org/api to <= 0.93.0 if you need to stay on an older version of storage. If this doesn't work, please file an issue rather than adding a PR comment here. |
thanks, @tritone :) |
Hi @codyoss and @tritone,
I added a helper func to handle different types (int64 and int64*) when reading data from Storage Apiary client.
I didn't add from Manual -> Apiary client because I need some help typecasting without Go yelling at me.
Towards unblocking: #6204